unit Unit7;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ComCtrls,
  StrUtils, Dialogs, Grids, AdvObj, BaseGrid, AdvGrid, DBAdvGrid, DB, ADODB, StdCtrls,
  FileCtrl, Mask, SkinBoxCtrls, spSkinShellCtrls, AdvEdit, AdvEdBtn,
  RzShellDialogs, WinXP, XPMan;

type
  TForm7 = class(TForm)
    ADOConnection1: TADOConnection;
    Query1: TADOQuery;
    DataSource1: TDataSource;
    Button1: TButton;
    Display: TProgressBar;
    Label1: TLabel;
    DLSource: TspSkinDirectoryEdit;
    Label2: TLabel;
    DLDest: TspSkinDirectoryEdit;
    Label3: TLabel;
    Label4: TLabel;
    DBSource: TAdvEditBtn;
    OD: TRzOpenDialog;
    WinXP1: TWinXP;
    XPManifest1: TXPManifest;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure DBSourceClickBtn(Sender: TObject);
  private
    { Private declarations }
    function getConnection: boolean;
    function CekInput(var AResult: boolean; vState: boolean; vMsg: string; vControl: TWinControl): boolean;

    function isValid: boolean;
    procedure Start;
    procedure StartFromDB;
  public
    { Public declarations }
  end;
  PProgressbar = ^TProgressBar;
var
  Form7: TForm7;
implementation

{$R *.dfm}
procedure TurboCopyFile(
  const
    SourceFile, (* the source file *)
    DestinationFile: String; (* destination file *)
  Display: PProgressbar (* this is a pointer to progress bar
    which we update if assigned,
    this procedure can be called without a
    progress bar like so:
    TurboCopyFile(Source, Destination, NIL); *)
  );
type
  (* define an array of 4096 bytes which holds the bytes *)
  TurboBuffer = array[1..4096] of Byte;
const
  (* we need to set the size of the buffer in a constant *)
  szBuffer = sizeof(TurboBuffer);
var
  (* File streams with which we work *)
  InStream,
  OutStream: TFileStream;
  (* we need this to store a logical operation's result *)
  CanCopy: Boolean;
  (* holds the number of bytes left when
     InStream.Size -InStream.Position < 4096 bytes *)
  BytesLeft: Integer;
  (* the Buffer almighty *)
  Buffer: TurboBuffer;
begin
  (* Open the source file so we can read it's bytes *)
  InStream := TFileStream.Create(SourceFile, fmOpenRead);
  (* create a new file to the desired destination *)
  OutStream := TFileStream.Create(DestinationFile, fmCreate);

  (* check if a progress bar was passed *)
  if Assigned(Display) then
    Display^.Max := InStream.Size;

  (* this is the operation I was talking about in the
     variable section, this checks if we can read a
     full buffer(4096 bytes) *)
  CanCopy :=
    (InStream.Size > InStream.Position) and
    ((InStream.Size -InStream.Position) >= szBuffer);

  (* this ensures that the progress bar is being painted *)
  Application.ProcessMessages;

  (* loop while we CanCopy *)
  while CanCopy do begin
    (* read 4096 bytes from source file *)
    InStream.ReadBuffer(Buffer, szBuffer);
    (* then write it to destination file *)
    OutStream.WriteBuffer(Buffer, szBuffer);
    (* check if display is assigned *)
    if Assigned(Display) then
      (* update it's position *)
      Display^.Position := InStream.Position;
    CanCopy :=
      (InStream.Size > InStream.Position) and
      ((InStream.Size -InStream.Position) >= szBuffer);
  end;

  (* store the number of bytes that is less than 4096 into
     a local variable *)
  BytesLeft := InStream.Size -InStream.Position;

  (* do we have some bytes left ? we don't care how many,
     we just know it's less than 4096 bytes *)
  if BytesLeft > 0 then begin
    (* surprise, or not we have some bytes left,
       read them all into the buffer *)
    InStream.ReadBuffer(Buffer, BytesLeft);
    (* write them to destination file *)
    OutStream.WriteBuffer(Buffer, BytesLeft);
  end;

  (* do we have a progress bar ? *)
  if Assigned(Display) then
    (* we do? then update it's progress *)
    Display^.Position := Display^.Max;

  (* free the memory *)
  FreeAndNil(InStream);
  FreeAndNil(OutStream);
end; (* end of TurboCopyFile *)


procedure TForm7.Button1Click(Sender: TObject);
begin
  if isValid then StartFromDB;
end;

function TForm7.CekInput(var AResult: boolean; vState: boolean; vMsg: string;
  vControl: TWinControl): boolean;
begin
    Result:= vState;
    AResult:= vState;
    if Result = true then Exit;
    MessageDlg(vMsg, mtWarning,[mbOk], 0);
    vControl.SetFocus;
end;

procedure TForm7.DBSourceClickBtn(Sender: TObject);
begin
  if OD.Execute then
    DBSource.Text := OD.FileName;
end;

procedure TForm7.FormCreate(Sender: TObject);
begin
//  DBSource.ReadOnly := true;
  Label1.Caption := '';
end;

function TForm7.getConnection: boolean;
var ConnStr: String;
begin
  try
//    ConnStr := 'Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source='+DBSource.Text;
    ConnStr := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+DBSource.Text+';Persist Security Info=False';
    ADOConnection1.ConnectionString := ConnStr;
    ADOConnection1.Connected := true;
    Query1.Close;
    Query1.SQL.Clear;
    Query1.SQL.Add('SELECT title, search, artist1, artist1_search, ''KANJI'' AS Kat, music_channel FROM song ');
    Query1.Open;
    Result := true;
  except
    Result := false;
  end;
end;

function TForm7.isValid: boolean;
begin
Result:= False;
//  if CekInput(Result, RoomID.Text <> '', 'Kode Room belum diisi.', RoomID) then
  if CekInput(Result, getConnection=true,'Gagal load database ...!', DBSource) then
  if CekInput(Result, DLSource.Text <> '', 'Direktori sumber belum diisi.', DLSource) then
     CekInput(Result, DLDest.Text <> '', 'Direktori tujuan belum diisi.', DLDest);
end;

procedure TForm7.Start;
var SrcFile, DestFile,
    Title, Search, Artist, Artist2, Kat, Channel : String;
    i : integer;
begin
{  for i := 1 to Grid1.RowCount do begin
    try
      Title := ''; Search := ''; Artist := ''; Artist2 := ''; Kat := ''; Channel := '';
      Title   := Grid1.Cells[1, i];
      Search  := Grid1.Cells[2, i];
      Artist  := Grid1.Cells[3, i];
      Artist2 := Grid1.Cells[4, i];
      Kat     := Grid1.Cells[5, i];
      Channel := IfThen(Grid1.Cells[6, i]='B','STEREO',IfThen(Grid1.Cells[6, i]='L','LEFT','RIGHT'));

//      SrcFile := 'L:\Chinese VCD1\'+Grid1.Cells[1, i]+'-'+Grid1.Cells[3, i]+'.avi';
      SrcFile := DLSource.Text+'\'+Grid1.Cells[1, i]+'-'+Grid1.Cells[3, i]+'.avi';
//      DestFile:= Concat('L:\RESULT RENAME\',Title,'#',Search,'#',Artist,'#',Artist2,'#',Kat,'#',Channel,'.mpg');
      DestFile:= Concat(DLDest.Text,'\',Title,'#',Search,'#',Artist,'#',Artist2,'#',Kat,'#',Channel,'.mpg');
      Label1.Caption := 'Copying '+SrcFile+'....';
      if FileExists(SrcFile) then
        TurboCopyFile(SrcFile, DestFile, @Display);
    finally
      Display.Position := 0;
    end;
  end;     }
end;
procedure TForm7.StartFromDB;
var SrcFile, DestFile,
    Title, Search, Artist, Artist2, Kat, Channel : String;
    i : integer;
begin
  Query1.First;
  for i := 0 to Query1.RecordCount -1 do begin
    try
      Title := ''; Search := ''; Artist := ''; Artist2 := ''; Kat := ''; Channel := '';
      Title   := Query1.Fields[0].AsString;
      Search  := Query1.Fields[1].AsString;
      Artist  := Query1.Fields[2].AsString;
      Artist2 := Query1.Fields[3].AsString;
      Kat     := Query1.Fields[4].AsString;
      Channel := IfThen(Query1.Fields[5].AsString='B','STEREO',IfThen(Query1.Fields[5].AsString='L','LEFT','RIGHT'));

//      SrcFile := 'L:\Chinese VCD1\'+Grid1.Cells[1, i]+'-'+Grid1.Cells[3, i]+'.avi';
      SrcFile := DLSource.Text+'\'+Title+'-'+Artist+'.avi';
//      DestFile:= Concat('L:\RESULT RENAME\',Title,'#',Search,'#',Artist,'#',Artist2,'#',Kat,'#',Channel,'.mpg');
      DestFile:= Concat(DLDest.Text,'\',Title,'#',Search,'#',Artist,'#',Artist2,'#',Kat,'#',Channel,'.mpg');
      Label1.Caption := 'Copying '+SrcFile+'.. '+IntToStr(i+1)+' From '+IntToStr(Query1.RecordCount);
      if FileExists(SrcFile) then
        TurboCopyFile(SrcFile, DestFile, @Display);
    finally
      Display.Position := 0;
      Query1.Next;
    end;
  end;
end;

end.
